// pages/mdtOrder/index.js
import {
  ApiPatientCardList,
  getDiseaseOrder,ApiMDTOrder
} from '../../api/index'
const app = getApp()
let {
  baseUrl,
  commonUpdate,
  commonUpload
} = app.globalData
Page({

  /**
   * 页面的初始数据
   */
  data: {
    show:false,
    fileArr: [],
    photoArr: [],
    fileListArr: [],
    videoArr: [],
    baseUrl,
    commonUpdate,
    commonUpload,
    lableIndex: 0,
    autosize: {
      maxHeight: 120,
      minHeight: 80
    },
    lableArr: [{
        lable: '发热',
        value: false
      },
      {
        lable: '关节酸痛',
        value: false
      },
      {
        lable: '通风',
        value: false
      },
      {
        lable: '腹痛',
        value: false
      },
      {
        lable: '空气过敏',
        value: false
      },
      {
        lable: '鼻塞',
        value: false
      },
      {
        lable: '头疼',
        value: false
      },
      {
        lable: '咳嗽',
        value: false
      },
    ],
    imgArr: [{
      lable: '病历',
      icon: 'label-o',
      arr: 'fileArr'
    }, {
      lable: '图片/视频',
      icon: 'photo-o',
      arr: 'photoArr'
    }, {
      lable: '文件',
      icon: 'description-o',
      arr: 'fileListArr'
    }, {
      lable: '录音',
      icon: 'bullhorn-o',
      arr: 'videoArr'
    }]
  },

  handleDelete(event) {
    let val = event.currentTarget.dataset.v
    const index = this.data.fileArr.findIndex(item => {
      return item == val
    })
    this.data.fileArr.splice(index, 1)
    this.setData({
      'fileArr': this.data.fileArr
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      selectData: JSON.parse(options.data),
      num: Number(JSON.parse(options.data).price) * 100
    })

  },


  onclickIncrease() {
    this.setData({
      show: true
    })
  },
  //获取需要咨询的项
 /*  handleTap(e) {
    let text = e.currentTarget.dataset.v
    let query = {
      count: 1,
      mediaType: ['image'],
      sourceType: ['album'],
      camera: 'back',
    }
    console.log(text)
    if (text.lable == '图片/视频') {
      this.photoCommon(query, '图片/视频')
    } else if (text.lable == '病历') {
      this.photoCommon(query, '病历')
    } else if (text.lable == '文件') {
      this.photoCommon(query, '文件')
    } else if (text.lable == '录音') {
      this.photoCommon(query, '录音')
    } else {
      return
    }
  }, */
/*   photoCommon(query, val) {
    console.log(query)
    let that = this
    let json = JSON
    wx.chooseMedia({
      ...query,
      success(res) {
        console.log('11111', res)
        that.setData({
          type: res.type
        })
        wx.showLoading({
          title: `正在上传${val}`,
          mask: true
        })
        let com = commonUpdate
        wx.uploadFile({
          url: baseUrl + com,
          filePath: res.tempFiles[0].tempFilePath,
          header: {
            Authorization: 'Bearer ' + wx.getStorageSync('token')
          },
          name: 'file',
          success(res) {
            let baseUrlLink = json.parse(res.data).data.path
            wx.showToast({
              title: "上传成功",
            })
            let dataArr = that.data.imgArr.find(item => {
              return item.lable == val
            })
            console.log(dataArr)
            that.data[dataArr.arr].push(baseUrlLink)
            that.setData({
              [dataArr.arr]: that.data[dataArr.arr]
            })

          },
          fail(err) {
            console.log(err)
          }
        })
      },
      // fail(res) {
      //   console.log(res)
      //   console.log('图片拍摄失败')
      // }
    })
  }, */



  getUploadImage(e) {
    let eVal = e.detail.value
    this.data.fileArr.push(eVal)
    this.setData({
      'fileArr': this.data.fileArr
    })
  },
  showData(e){
    this.setData({
      'show': e.detail
    })
  },

  lableClick(e) {
    let item = e.currentTarget.dataset.v
    let index = e.currentTarget.dataset.index
    this.setData({
      [`lableArr[${index}].value`]: !item.value
    })
    console.log(this.data.lableArr)
  },
  clickPatientCardList(e) {
    let index = e.currentTarget.dataset.index
    this.setData({
      active: index,
      defaultPatient: this.data.patientCardList[index]
    })
  },
  createPatientCard() {
    wx.navigateTo({
      url: '/pages/patientCard/index',
    })
  },

  getPatientList() {
    let that = this
    wx.showLoading({
      title: '加载中'
    })
    ApiPatientCardList().then(res => {
      res.data.forEach((item, index) => {
        if (item.use == '1') {
          that.setData({
            defaultPatient: item,
            active: index
          })
          wx.hideLoading()
        }
      })
      this.setData({
        patientCardList: res.data
      })
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.getPatientList()
  },

  onChangeDis(e) {
    this.setData({
      description: e.detail
    })
  },

  onSubmit() {
    let that = this
    const label = this.data.lableArr.map(item => {
      if (item.value) {
        return item.lable
      }
    }).filter(i => i != null)
    let query = {
      mdt_disease_id: this.data.selectData.id,
      patient_id: this.data.defaultPatient.id,
      description: this.data.description,
      files: this.data.fileArr.toString(),
      label: label.toString()
    }
    getDiseaseOrder(query).then(res => {
      if (res.code == '200') {
        that.submitConsultation(res.data)
      }
    })
  },

  submitConsultation(data) {
    let that = this
    this.setData({
      isLoading: true
    })
    ApiMDTOrder(data.order.id).then(res => {
      if (res.code == '200') {
        //删除storage
        /*   wx.removeStorage({
            key: 'priceDetail',
          }) */
        wx.requestPayment({
          ...res.data,
          success(res) {
            that.setData({
              isLoading: false
            })
            wx.reLaunch({
              url: '/pages/mdtList/index',
            })
          },
          fail(res) {
            that.setData({
              isLoading: false
            })
            wx.reLaunch({
              url: '/pages/mdtList/index',
            })
            /*   wx.reLaunch({
                url: '/pages/message/index',
              }) */
          }


        })


      }
    })
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})